//
//  JGLoginController.m
//  JGRider
//
//  Created by 单启志 on 2018/3/2.
//  Copyright © 2018年 ffq. All rights reserved.
//

#import "JGLoginController.h"
#import "JGLoginContentView.h"
#import "JGValidateLoginController.h"
#import "JGForgetPwdController.h"
#import "JGRegistController.h"
#import "JGLoginResponseModel.h"
#import "JGCodingHelper.h"
#import "JGRootViewHelper.h"
#import "JGLoginApi.h"
@interface JGLoginController ()<JGLoginContentViewDelegate,YTKChainRequestDelegate>
@property(nonatomic,strong)JGLoginContentView *contentView;
@property(nonatomic,strong)MBProgressHUD *hud;
@end

@implementation JGLoginController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.contentView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

#pragma mark - override
-(UIColor *)lmjNavigationBackgroundColor:(LMJNavigationBar *)navigationBar{
    return [UIColor whiteColor];
}

- (UIStatusBarStyle)navUIBaseViewControllerPreferStatusBarStyle:(LMJNavUIBaseViewController *)navUIBaseViewController
{
    return UIStatusBarStyleDefault;
}

#pragma mark - contentView delegate
//登录
-(void)contentViewLoginEventWithParams:(NSDictionary *)params{

    [[JGRequestHelper share]requestWithParams:params requestApi:[JGLoginApi class] hudMessage:@"登录中..." view:self.view successBlock:^(id result) {
        
        NSString *states=result[@"data"][@"state"];
        if ([states isEqualToString:@"11000"]) {
            //登录成功
            NSDictionary *dic= result[@"data"][@"obj"];
            JGLoginResponseModel *model=[JGLoginResponseModel mj_objectWithKeyValues:dic];

            //归档登录成功获取的数据
            [[JGCodingHelper share]loginsuccessDataEncodeWith:model];
            [JGRootViewHelper setTabBarViewController];
        }

    } faileBlock:nil];

//    _hud=[MBProgressHUD showMessage:@"登录中..." ToView:self.view];
//    FQGetSignApi *signApi=[[FQGetSignApi alloc]initWithParams:params];
//    FQChainRequest *chinaReq=[[FQChainRequest alloc]init];
//    [chinaReq addRequest:signApi callback:^(YTKChainRequest * _Nonnull chainRequest, YTKBaseRequest * _Nonnull baseRequest) {
//        FQGetSignApi *api= (FQGetSignApi*)baseRequest;
//        JGLoginApi *loginApi=[[JGLoginApi alloc]initWithSi:[api si] Pa:[api pa]];
//        [chainRequest addRequest:loginApi callback:nil];
//    }];
//    chinaReq.delegate=self;
//    [chinaReq start];

}
//验证码登录
-(void)contentViewValidateLoginEvent{
    JGValidateLoginController *vc=[[JGValidateLoginController alloc]init];
    JGpushViewController(vc);
}
//注册
-(void)contentViewRegistEvent{
    JGRegistController *vc=[[JGRegistController alloc]init];
    JGpushViewController(vc);
}
//忘记密码
-(void)contentViewForgetPwdEvent{
    JGForgetPwdController *vc=[[JGForgetPwdController alloc]init];
    JGpushViewController(vc);
}
#pragma mark api - delegate
-(void)chainRequestFinished:(YTKChainRequest *)chainRequest{

   JGLoginApi *api=(JGLoginApi*)chainRequest.requestArray[1];

   NSString *msg=api.responseJSONObject[@"data"][@"msg"];
    NSString *states=api.responseJSONObject[@"data"][@"state"];
    if ([states isEqualToString:@"11000"]) {
        //登录成功
        NSDictionary *dic= api.responseJSONObject[@"data"][@"obj"];
       JGLoginResponseModel *model=[JGLoginResponseModel mj_objectWithKeyValues:dic];

        //归档登录成功获取的数据
        [[JGCodingHelper share]loginsuccessDataEncodeWith:model];
        [JGRootViewHelper setTabBarViewController];
    }
    _hud.label.text=msg;
    [_hud hideAnimated:YES afterDelay:0.5];
    NSLog(@"%@",api.responseString);
}
-(void)chainRequestFailed:(YTKChainRequest *)chainRequest failedBaseRequest:(YTKBaseRequest *)request{

    _hud.label.text=@"登录失败";
    [_hud hideAnimated:YES afterDelay:0.5];
}

#pragma mark - getter setter
-(JGLoginContentView *)contentView{

    if (_contentView==nil) {
        _contentView=[[JGLoginContentView alloc]initWithFrame:CGRectMake(0, self.lmj_navgationBar.height, JGScreenW, JGScreenH-self.lmj_navgationBar.height)];
        _contentView.delegate=self;
    }
    return _contentView;
}

@end
